<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <!-- <el-form-item label="专业编号" prop="professionNumber">
        <el-input
          v-model="form.professionNumber"
          placeholder="请输入专业编号"
        />
      </el-form-item> -->
      <el-form-item label="专业名称" prop="professionName">
        <el-input v-model="form.professionName" placeholder="请输入专业名称" />
      </el-form-item>
      <el-form-item label="英文名称" prop="englishName">
        <el-input v-model="form.englishName" placeholder="请输入英文名称" />
      </el-form-item>
      <!-- <el-form-item label="专业代码" prop="professionCode">
        <el-input v-model="form.professionCode" placeholder="请输入专业代码" />
      </el-form-item> -->
      <el-form-item label="学院" prop="collegeId">
        <college-selector v-model="form.collegeId"></college-selector>
      </el-form-item>
      <el-form-item label="系部" prop="departmentsId">
        <departments-selector
          v-model="form.departmentsId"
          :college-id="form.collegeId"
        ></departments-selector>
      </el-form-item>
      <el-form-item label="负责人" prop="userId">
        <el-input v-model="form.userId" placeholder="请输入负责人" />
        <!-- <select-user v-model="form.userId"></select-user> -->
      </el-form-item>
      <el-form-item label="培养层级" prop="hierarchy">
        <el-select v-model="form.hierarchy" placeholder="请选择培养层级">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.STUDENT_TRAINING_LEVEL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="修习学年" prop="schoolYear">
        <el-select v-model="form.schoolYear" placeholder="请选择修习学年">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.SCHOOL_YEAR)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开设时间" prop="openingTime">
        <el-date-picker
          clearable
          v-model="form.openingTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择开设时间"
        />
      </el-form-item>
      <!-- <el-form-item label="机构编号" prop="orgId">
        <el-input v-model="form.orgId" placeholder="请输入机构编号" />
      </el-form-item> -->
      <el-form-item label="专业简介" prop="introduce">
        <editor v-model="form.remark" :min-height="192" />
      </el-form-item>
      <!-- <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注" />
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  createProfessions,
  updateProfessions,
} from "@/api/collegebase/professions";
import common from "@/api/collegebase/common";
import Editor from "@/components/Editor";
import CollegeSelector from "@/components/CollegeSelector";
import DepartmentsSelector from "@/components/DepartmentsSelector";
import selectUser from '@/components/CollegeSelectUser/CollegeSelectUser.vue'
export default {
  name: "ProfessionsEdit",
  components: { DepartmentsSelector, CollegeSelector, Editor, selectUser },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {

        professionName: [
          { required: true, message: "请输入专业名称", trigger: "blur" },
        ],
        collegeId: [
          { required: true, message: "请选择学院", trigger: "blur" },
        ],
        departmentsId: [
          { required: true, message: "请选择系部", trigger: "blur" },
        ],
        userId: [{ required: true, message: "请输入负责人", trigger: "blur" }],
        hierarchy: [
          { required: true, message: "请选择培养层级", trigger: "blur" },
        ],
        schoolYear: [
          { required: true, message: "请选择修习学年", trigger: "blur" },
        ],
        openingTime: [
          { required: true, message: "请选择开设时间", trigger: "blur" },
        ],
      },
    };
  },
  created() {
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
  },
  methods: {

    //获取数据

    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime];

      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:show", false);
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        professionNumber: undefined,
        professionName: undefined,
        professionCode: undefined,
        collegeId: undefined,
        departmentsId: undefined,
        userId: undefined,
        hierarchy: undefined,
        openingTime: undefined,
        orgId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateProfessions(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit("success");
          });
          return;
        }
        // 添加的提交
        createProfessions(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },
  },
};
</script>
